package c.j.a.d.b.j;

import android.database.sqlite.SQLiteException;
import android.os.Build.VERSION;
import android.os.Looper;
import android.os.Message;
import android.util.SparseArray;
import c.j.a.d.a.i;
import c.j.a.d.b.c.e;
import c.j.a.d.b.c.g;
import c.j.a.d.b.c.g.d;
import c.j.a.d.b.e.n;
import c.j.a.d.b.e.o;
import c.j.a.d.b.e.p;
import c.j.a.d.b.e.t;
import c.j.a.d.b.m.f;
import com.ss.android.socialbase.downloader.g.c;
import java.util.ArrayList;
import java.util.List;

/* compiled from: DefaultDownloadCache */
public class h implements n {
    public final p a;
    public t b;
    public volatile boolean c;
    public volatile boolean d;
    public c.j.a.d.b.m.f.a e = new a();
    public f f = null;

    /* compiled from: DefaultDownloadCache */
    public class a implements c.j.a.d.b.m.f.a {

        /* compiled from: DefaultDownloadCache */
        public class a implements Runnable {
            public void run() {
                try {
                    h.this.q();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        public void a(Message message) {
            if (message.what == 1) {
                c.j.a.d.b.e.b.q().execute(new a());
            }
        }
    }

    /* compiled from: DefaultDownloadCache */
    public class b implements d {
    }

    public h() {
        p pVar = new p();
        this.a = pVar;
        if (!c.j.a.d.b.l.a.f.k("fix_sigbus_downloader_db", false)) {
            this.b = new e();
        } else if (c.j.a.d.b.n.a.r()) {
            this.b = new e();
        } else {
            g gVar = new g();
            gVar.d = new b();
            this.b = gVar;
        }
        this.c = false;
        this.f = new f(Looper.getMainLooper(), this.e);
        c.j.a.d.b.e.b.h(c.j.a.d.b.b.d.SYNC_START);
        this.b.y0(pVar.a, pVar.b, new i(this));
    }

    public c C(int i, long j) {
        c b = this.a.b(i);
        if (b != null) {
            b.p(j, false);
            b.b0(-2);
        }
        d(i, null);
        return b;
    }

    public void E(com.ss.android.socialbase.downloader.g.b bVar) {
        if (c.j.a.d.b.n.a.E()) {
            p a = q.a(true);
            if (a != null) {
                a.f(bVar);
                return;
            } else {
                this.b.f(bVar);
                return;
            }
        }
        this.b.f(bVar);
    }

    public c I(int i, long j, String str, String str2) {
        c I = this.a.I(i, j, str, str2);
        c(I, true);
        return I;
    }

    public c O(int i, long j) {
        c b = this.a.b(i);
        if (b != null) {
            b.p(j, false);
            b.b0(-1);
            b.V = false;
        }
        d(i, null);
        return b;
    }

    public List<c> a(String str) {
        return this.a.a(str);
    }

    public c b(int i) {
        return this.a.b(i);
    }

    public boolean c() {
        return this.c;
    }

    public boolean d() {
        if (this.c) {
            return true;
        }
        synchronized (this) {
            if (!this.c) {
                c.j.a.d.b.g.a.f("DefaultDownloadCache", "ensureDownloadCacheSyncSuccess: waiting start!!!!");
                try {
                    wait(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                c.j.a.d.b.g.a.f("DefaultDownloadCache", "ensureDownloadCacheSyncSuccess: waiting end!!!!");
            }
        }
        return this.c;
    }

    public c e(int i) {
        c b = this.a.b(i);
        if (b != null) {
            b.b0(2);
        }
        c(b, true);
        return b;
    }

    public void f(com.ss.android.socialbase.downloader.g.b bVar) {
        this.a.f(bVar);
        if (c.j.a.d.b.n.a.E()) {
            p a = q.a(true);
            if (a != null) {
                a.f(bVar);
                return;
            } else {
                this.b.f(bVar);
                return;
            }
        }
        this.b.f(bVar);
    }

    public c g(int i) {
        c g = this.a.g(i);
        c(g, true);
        return g;
    }

    public c h(int i) {
        c b = this.a.b(i);
        if (b != null) {
            b.b0(1);
        }
        c(b, true);
        return b;
    }

    public void i(int i, List<com.ss.android.socialbase.downloader.g.b> list) {
        if (list != null && list.size() != 0) {
            this.a.i(i, list);
            if (c.j.a.d.b.n.a.M()) {
                this.b.d(i, list);
            }
        }
    }

    public List<c> j(String str) {
        return this.a.j(str);
    }

    public void k(int i, int i2, long j) {
        this.a.k(i, i2, j);
        if (c.j.a.d.b.n.a.E()) {
            p a = q.a(true);
            if (a != null) {
                a.k(i, i2, j);
                return;
            } else {
                this.b.k(i, i2, j);
                return;
            }
        }
        this.b.k(i, i2, j);
    }

    public boolean l(int i) {
        try {
            if (c.j.a.d.b.n.a.E()) {
                p a = q.a(true);
                if (a != null) {
                    a.T(i);
                } else {
                    this.b.l(i);
                }
            } else {
                this.b.l(i);
            }
        } catch (SQLiteException e) {
            e.printStackTrace();
        }
        this.a.l(i);
        return true;
    }

    public void m(int i, int i2, int i3, long j) {
        if (c.j.a.d.b.n.a.E()) {
            p a = q.a(true);
            if (a != null) {
                a.m(i, i2, i3, j);
                return;
            } else {
                this.b.m(i, i2, i3, j);
                return;
            }
        }
        this.b.m(i, i2, i3, j);
    }

    public void n(int i, int i2, int i3, int i4) {
        if (c.j.a.d.b.n.a.E()) {
            p a = q.a(true);
            if (a != null) {
                a.n(i, i2, i3, i4);
                return;
            } else {
                this.b.n(i, i2, i3, i4);
                return;
            }
        }
        this.b.n(i, i2, i3, i4);
    }

    public c o(int i) {
        c b = this.a.b(i);
        if (b != null) {
            b.b0(-7);
        }
        c(b, true);
        return b;
    }

    public void p() {
        long j = c.j.a.d.b.l.a.f.k("task_resume_delay", false) ? 4000 : VERSION.SDK_INT >= 23 ? 1000 : 5000;
        this.f.sendMessageDelayed(this.f.obtainMessage(1), j);
    }

    public void q() {
        if (!this.c) {
            return;
        }
        if (this.d) {
            c.j.a.d.b.g.a.d("DefaultDownloadCache", "resumeUnCompleteTask: has resumed, return!!!");
            return;
        }
        this.d = true;
        if (c.j.a.d.b.n.a.r()) {
            o w = c.j.a.d.b.e.b.w();
            if (w != null) {
                ArrayList arrayList = new ArrayList(2);
                arrayList.add("application/vnd.android.package-archive");
                arrayList.add("application/ttpatch");
                if (!arrayList.isEmpty()) {
                    SparseArray sparseArray = this.a.a;
                    if (sparseArray != null) {
                        ArrayList arrayList2 = new ArrayList();
                        synchronized (sparseArray) {
                            for (int i = 0; i < sparseArray.size(); i++) {
                                int keyAt = sparseArray.keyAt(i);
                                if (keyAt != 0) {
                                    c cVar = (c) sparseArray.get(keyAt);
                                    if (cVar != null) {
                                        String str = cVar.u;
                                        if (str != null) {
                                            if (arrayList.contains(str)) {
                                                if (cVar.p0() != -2 || cVar.h()) {
                                                    cVar.G0 = false;
                                                    arrayList2.add(cVar);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (!arrayList2.isEmpty()) {
                            ((i.c) w).c(arrayList2, 1);
                        }
                    }
                }
            }
        }
    }

    public c t(int i, long j) {
        c t = this.a.t(i, j);
        d(i, null);
        return t;
    }

    public c a(int i, int i2) {
        c b = this.a.b(i);
        if (b != null) {
            b.R = i2;
        }
        c(b, true);
        return b;
    }

    public List<c> b(String str) {
        return this.a.b(str);
    }

    public List<c> c(String str) {
        return this.a.c(str);
    }

    public void b() {
        try {
            this.a.b();
        } catch (SQLiteException e) {
            e.printStackTrace();
        }
        if (c.j.a.d.b.n.a.E()) {
            p a = q.a(true);
            if (a != null) {
                a.f();
                return;
            } else {
                this.b.b();
                return;
            }
        }
        this.b.b();
    }

    public List<com.ss.android.socialbase.downloader.g.b> c(int i) {
        return (List) this.a.b.get(i);
    }

    public final void c(c cVar, boolean z) {
        if (cVar != null) {
            if (!c.j.a.d.b.n.a.E()) {
                this.b.a(cVar);
            } else if (z) {
                p a = q.a(true);
                if (a != null) {
                    a.c(cVar);
                } else {
                    this.b.a(cVar);
                }
            }
        }
    }

    public boolean a(c cVar) {
        if (cVar == null) {
            return false;
        }
        boolean a = this.a.a(cVar);
        c(cVar, true);
        return a;
    }

    public boolean f(int i) {
        if (c.j.a.d.b.n.a.E()) {
            p a = q.a(true);
            if (a != null) {
                a.F(i);
            } else {
                this.b.f(i);
            }
        } else {
            this.b.f(i);
        }
        p pVar = this.a;
        pVar.l(i);
        pVar.d(i);
        return true;
    }

    public c a(int i, long j) {
        c a = this.a.a(i, j);
        c(a, false);
        return a;
    }

    public void b(c cVar) {
        if (cVar != null) {
            this.a.a(cVar);
        }
    }

    public void d(int i) {
        this.a.d(i);
        if (c.j.a.d.b.n.a.E()) {
            p a = q.a(true);
            if (a != null) {
                a.y(i);
                return;
            } else {
                this.b.d(i);
                return;
            }
        }
        this.b.d(i);
    }

    public void d(int i, List<com.ss.android.socialbase.downloader.g.b> list) {
        try {
            List list2;
            a(this.a.b(i));
            if (list2 == null) {
                list2 = (List) this.a.b.get(i);
            }
            if (c.j.a.d.b.n.a.E()) {
                p a = q.a(true);
                if (a != null) {
                    a.d(i, list2);
                    return;
                } else {
                    this.b.d(i, list2);
                    return;
                }
            }
            this.b.d(i, list2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
